package com.qiwx.pigeon.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;

import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.SharedPreferencesUtil;
import com.qiwx.pigeon.activity.account.LoginActivity;
import com.qiwx.pigeon.base.BaseMVPActivity;
import com.qiwx.pigeon.data.params.LoginParam;
import com.qiwx.pigeon.data.vo.EmployBean;
import com.qiwx.pigeon.presenter.LoginPresenter;
import com.qiwx.pigeon.viewinterface.ILoginView;

import java.lang.ref.WeakReference;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;

/**
 * @description
 * @Author qiwx
 * @Date 2020-06-04 13:49
 **/
public class SplashActivity extends BaseMVPActivity<ILoginView, LoginPresenter> implements ILoginView {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);

    }

    @Override
    public LoginPresenter createPresenter() {
        return new LoginPresenter(new WeakReference(this));
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    public void goLogin() {
        Intent go = new Intent(this, LoginActivity.class);
        startActivity(go);
        finish();
    }

    public void goMain() {
        Intent go = new Intent(this, MainActivity.class);
        startActivity(go);
        finish();
    }


    @Override
    public void initAfterOnCreate() {
        String[] loginInfo = SharedPreferencesUtil.getLoginInfo(this);

        Observable.timer(1000, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread()).
                subscribe(aLong -> {
                    if (loginInfo == null ||
                            loginInfo[0] == null ||
                            loginInfo[1] == null
                    ) {
                        goLogin();
                    } else {
                        LoginParam param = new LoginParam();
                        param.setLogin_name(loginInfo[0]);
                        param.setPwd(loginInfo[1]);
                        mPresenter.login(param, false);
                    }
                });

    }

    @Override
    public void onSuccess(int type) {


    }

    @Override
    public void onLoginSuccess(EmployBean user) {
        SharedPreferencesUtil.setEmployBean(this, user);
        goMain();

    }

    @Override
    public void onFailure(String error) {
        goLogin();
    }
}
